// pages/me/article/index.js
const app = getApp();
const common = require('../../../utils/common.js');
Page({

  /**
   * 页面的初始数据
   */
    data: {
        currentTab: 0,
        
        page_charge:0,             //审核中文章页数标记
        page_consume:0,           //已发布文章页数标记
        chargePoints:[],            //充值记录
        consumePoints:[],       //交易记录
        show_consume:{},      //手风琴显示标记(交易记录用)
        show_charge: {},      //手风琴显示标记(充值记录用)
    },
    

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var that=this;
      wx.getSystemInfo({
          success: function(res) {
              console.log(res); //80rpx +4px
              var height = res.windowHeight+'px';
              that.setData({
                  clientHeight:height,
              })
          },
      })
      //获取列表
      var url_charge = `${app.apiHost}Mynotice/rechargePoints`;  //充值记录接口
      var url_consume = `${app.apiHost}Mynotice/consumePoints`;  //交易记录接口      
      this.getArticle(url_charge,'chargePoints');      
      this.getArticle(url_consume, 'consumePoints');      
  },
  //获取文章列表公共函数
  getArticle:function(url,datakey,page=0){      //page:页数
      var is_login = common.checkLogin();
      if (!is_login) return;
      var that = this;
      var token = common.getToken();
      var userId = common.getUserId();
      var start = page++ * 10;

      var data = { token: token, userId: userId, start: start };

      var msg = '加载中';
      var func = function (content) {

          if (content.pointsList && content.pointsList.length > 0) {
              let data = {};
              if (page > 1) {
                  data[datakey] = that.data[datakey].concat(content.pointsList);       //加载更多页数显示方法
              } else {
                  data[datakey] = content.pointsList;
              }

              
              that.setData(data);
          } else {
              if (page > 1) {
                  common.toast('没有更多了');
              }
          }


      };
      var res = common.request(url, data, msg, func, that);  
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

 
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  swichNav: function (e) {
      if (this.data.currentTab === e.target.dataset.current) {
          return false;
      } else {
          this.setData({
              currentTab: e.target.dataset.current
          })
      }
  },
  bindChange: function (e) {
      this.setData({ currentTab: e.detail.current });
  },
  //审核中文章加载更多
  more_examine:function(){
      
      //获取列表
      var page=this.data.page_articleList_examine;
      var url_examine = `${app.apiHost}Myarticle/examine`;  //审核中文章列表接口
         
      this.getArticle(url_examine, 'articleList_examine',page);
      
  },
  //已发布文章加载更多
  more_published: function () {

      //获取列表
      var page = this.data.page_articleList_published;
      var url_examine = `${app.apiHost}Myarticle/published`;  //审核中文章列表接口

      this.getArticle(url_examine, 'articleList_published', page);

  },
  //手风琴效果(交易记录)
  shoufengqin_consume:function(e){
        var id=e.currentTarget.dataset.id;
        
        //if(!id) return;
        var index=id;       //完全是为了语意明显
        var show = this.data.show_consume[`show${index}`];
        
        if (typeof (show)==undefined){
            
        }else{
            if(show){                
                let data = {};
                let show = {};
                show[`show${index}`] = false;
                data.show_consume = show;
                data.id_consume = id;
                this.setData(data)
            }else{
                let data = {};
                let show = {};
                show[`show${index}`] = true;
                data.show_consume = show;
                data.id_consume = id;
                this.setData(data)
            }
        }
        
  },
  //手风琴效果(充值记录)
  shoufengqin_charge: function (e) {
      var id = e.currentTarget.dataset.id;

      //if(!id) return;
      var index = id;       //完全是为了语意明显
      var show = this.data.show_charge[`show${index}`];

      if (typeof (show) == undefined) {

      } else {
          if (show) {
              let data = {};
              let show = {};
              show[`show${index}`] = false;
              data.show_charge = show;
              data.id_charge = id;
              this.setData(data)
          } else {
              let data = {};
              let show = {};
              show[`show${index}`] = true;
              data.show_charge = show;
              data.id_charge = id;
              this.setData(data)
          }
      }

  },
})